<%@page import="com.kh.pajx.sop.util.common.FormatDate"%>
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="f"%>
<%
	String path = request.getContextPath();
	String temp_str = FormatDate.getUseDate(new Date());
%>
<script language="javascript" type="text/javascript" src="${pageContext.request.contextPath}/script/js/dictionary.js"></script>
<script type="text/javascript">
var initCostFlag=true,billCodeNode= new Dictionary();//创建一个map对象
var codeList=new Array();
function allCheck(check) {//全选
   　var checkbox=document.getElementsByName("cost");
        if(check.checked){
				for(var i=0;i<checkbox.length;i++){
　						checkbox[i].checked="checked";
               }
		}else{
               for(var i=0;i<checkbox.length;i++){
　						checkbox[i].checked="";
				} 
		}  
}
  $(document).ready(function(){
		    	  $("#saleId").val('${saleId}'); 
  });
  function immediately(element,str,str1){
	 var sumfee=document.getElementById("sumfee"+str);
	　var cast=document.getElementsByName("cast");
	var tem=0.0;
	  if("\v"=="v") {
	     element.onpropertychange = webChange;
	  }else{
	   element.addEventListener("input",webChange,false);
	  }
	  function webChange(){
	    if(element.value){
	    	};
	  }
	  if(!isNaN(element.value)&&element.value>=0){
	      for(var i=0;i<cast.length;i++){
 		     tem+=cast[i].value*1;
		   } 
	       sumfee.value=Math.round(tem*10)/10;
	  }else{
		  alertMsg.error("输入值错误！");
		  element.value="0.0";
		  element.focus();
		  for(var i=0;i<cast.length;i++){
	 		     tem+=cast[i].value*1;
			   } 
		       sumfee.value=Math.round(tem*10)/10;
	  }
	}
	  function showTr(sid,sindex,state,money){
		  for (var i=0;i<10;i++){
				if(sindex==i)
				   $("#tr"+i).css("background-color","#99CCCC");
			   else
				   $("#tr"+i).css("background-color","white");
			}
			var tr ="";
			var whichEl = document.getElementById("tr" + sid);
			var datalist=$("#balanceAppList"+sid);
			datalist.empty();
			if (whichEl.style.display == "none") {
				if(state==1){
					var data={"foreign.appBillCode":sid};
					var url="foreign_findForeignList.action";
			$.post(url,data,function(message){
				var list=message;
				var str=list.split(";");
				codeList = {};
				for ( var i = 0; i < str.length-1; i++) {
					var str1=str[i].split(",");
					tr+="<tr>";
					codeList[i]=str1[0];
					for ( var j = 2; j< str1.length-1; j++) {
						if(j==10||j==12||j==14||j==16){
								var val=str1[0]+j;
								tr +="<td><span ><input type='text' style='width: 60px;color:red;'  oninput='immediately(this,"
									+str1[1]
								    +","+"\""
								    +val+"\""
									+")';  name='cast' onpropertychange='immediately(this,"
									+","+"\""
								    +val+"\""
									+")' class='billcode"
									+str1[0]
									+"' id='cast" 
								     +val
								    +"' value='"
									 +str1[j]
								 +"'/></span></td>"; 
					   }else if(j==17){
						   tr+="<td><textarea rows='' cols='' class='billcode"
						         +str1[0]
						         +"' id='desc' >"
								 +str1[17]
								 +"</textarea></td>"; 
					   }else if(j==2){
						   tr+="<td><a href='javascript:void(0)' onclick='alertDiv("
						    +1+","+"\""+str1[18]+"\""
							+")'>"
							 +str1[2]
							 +"</a></td>"; 
					   }else{
						   tr+="<td>"
								 +str1[j]
								 +"</td>"; 
						   }
						
					}
					tr+="</tr>";
				}
				datalist.append(tr);
				whichEl.style.display = "";
				initRemoveAllNode(sid,money);
			     })
				}else{
					var data={"areaManger.appBillCode":sid};
					var url="foreign_findAreaMangerList.action";
					$.post(url,data,function(message){
						var list=message;
						var str=list.split(";");
						codeList = {};
						for ( var i = 0; i < str.length-1; i++) {
							var str1=str[i].split(",");
							tr+="<tr>";
							codeList[i]=str1[0];
							for ( var j = 2; j< str1.length-1; j++) {
								if(j==11){
										var val=str1[0]+j;
										tr +="<td><span ><input type='text' style='width: 60px;color:red;'  oninput='immediately(this,"
											+str1[1]
										    +","+"\""
										    +val+"\""
											+")';  name='cast' onpropertychange='immediately(this,"
											+","+"\""
										    +val+"\""
											+")' class='billcode"
											+str1[0]
											+"' id='cast" 
										     +val
										    +"' value='"
											 +str1[j]
										 +"'/></span></td>"; 
							   }else if(j==12){
								   tr+="<td><textarea rows='' cols='' class='billcode"
								         +str1[0]
								         +"' id='desc' >"
										 +str1[j]
										 +"</textarea></td>"; 
							   }else if(j==2){
								   tr+="<td><a href='javascript:void(0)' onclick='alertDiv("
									    +2+","+"\""+str1[13]+"\""
									    +")'>"
										 +str1[2]
										 +"</a></td>"; 
								}else{
								   tr+="<td>"
										 +str1[j]
										 +"</td>";    
							   }
								
							}
							tr+="</tr>";
						}
						datalist.append(tr);
						whichEl.style.display = "";
						initRemoveAllNode(sid,money);
					})
					}
			}else{
				 initRemoveNode(sid,money);
			}
		}
		function  initRemoveNode(sid,money){
			var whichEl = document.getElementById("tr" + sid);
			whichEl.style.display = "none";
			$("#sumfee"+sid).val(money);
		}
		function  initRemoveAllNode(sid,money){
			　var whichEl=document.getElementsByName("tr_code");
					for(var i=0;i<whichEl.length;i++){
						if(whichEl[i].value!=sid){
							var tr= document.getElementById("tr" + whichEl[i].value);
							if(tr.style.display = "none"){
							}else{
						   tr.style.display = "none";
						     $("#tr" + whichEl[i].value).empty();
							}
						}
					}
					$("#sumfee"+sid).val(money);
			 };
			 function save(){
				 var saveTrApp=$("#tableapp tr.selected").attr('saveTrApp');
					if(typeof(saveTrApp) != "undefined"){
						var tr=saveTrApp.split(",")[0];
						var ix=saveTrApp.split(",")[1];
						var uuid=saveTrApp.split(",")[2];
						var state=saveTrApp.split(",")[3];
				  var numCost=document.getElementById("sumfee"+tr).value;
				  var sum=document.getElementsByName("cast");
				  var sumvalue=0;
					for(var i=0;i<sum.length;i++){
			　				sumvalue+=sum[i].value*1;		
			        }
					if(sumvalue>0){
						numCost=sumvalue;
					}
					alertMsg.confirm("确定要审批吗？", {
						okCall : function() {
				  var str="";
				  for(var i in codeList){
					 var allMoney=$(".billcode"+codeList[i]);
					 var s="";
					  allMoney.each(function(j) {
						  if(this.value!="")
	　					    s+=this.value+",";
						  else
						    s+="0.0,";  
	                 })
					 str+=s+";";
				  }
				  var  url="foreign_addOneBalanceApp.action";
				  data={
						  "model.uuid":uuid,
						  "model.appType":1,
						  "model.billState":2,
						  "model.chkMoney":numCost,
						  "model.appState":state,
						  "dataCost":str
				  }
				  $.ajax({ 
					  url: url, 
					  type:"post",
					  data :data,
					  dataType:"json",
					  success: function(message){
						  navTabAjaxDone(message);
				      }});
						}})
					}else{
						alertMsg.info("请选择一条记录");
					}
			  };
			  function alertDiv(str1,str2){
				 if(str1==""||str2==""){
					 alertMsg.error("数据异常");
					 return;
				 }
				 $.pdialog.open("foreign_oldOrderList.action?orderType="+str1+"&orderUuid="+encodeURIComponent(str2),"oldList","单据列表","{width:968px,height:400px,max:true,mask:true,fresh:true}");
			  }
</script>
	<div class="pageHeader">
	<form id="pagerForm" method="post" action="foreign_balanceAppList.action">
	<input type="hidden" name="pageNum" value="${currentPage}" />
	<input type="hidden" name="numPerPage" value="${numPerPage}" />
	<input type="hidden"  value="${saleId}" name="saleId">
	</form>
				<form onsubmit="return navTabSearch(this);" action="foreign_balanceAppList.action" method="post" >
			<div class="searchBar nowrap">
			<p><label>部&nbsp;&nbsp;门：</label><select name="deptCode" id="deptCode" class="combox" ref="saleId"
														refUrl="school_getAllSaler.action?deptCode={value}">
									<option value="">---所有部门---</option>
									<c:forEach items="${deptList}" var="dept">
										<option ${deptCode==dept.deptCode?"selected":""} value="${dept.deptCode}">${dept.deptName}</option>
									</c:forEach>
							</select>
				</p>
				<p><label>销售人员</label>
							<select name="saleId" id="saleId"
								class="required combox">
									<option value="">---请选择---</option>
							</select>
				</p>
			<div class="subBar" float="left">
				<ul>
					<li>
					<div class="buttonActive" ><div class="buttonContent"><button type="submit">检索</button></div></div>
					</li>
				</ul>
			</div>
		</div>
			</form>
		</div>
<div class="pageContent">
	<div class="panelBar">
		<ul class="toolBar">
			<li><a class="add" href="javascript:void(0)" onclick="save()"><span>审批</span></a></li>
			<li class="line">line</li>
			
		</ul>             
	</div>
			<table class="list" width="100%" layoutH="96" id="tableapp">
				<thead>
					<tr>
						
						<th>申请单号</th>
						<th>申请类型</th>
						<th>申请人</th>
						<th>申请金额</th>
						<th>申请日期</th>
						<th>审批人</th>
						<th>审批日期</th>
						<th>审批金额</th>
					</tr>
				</thead>

				<tbody>
					<c:forEach items="${recordList}" var="fb" varStatus="countStatus">
						<tr id="tr${countStatus.count}" saveTrApp="${fb.appBillCode},${countStatus.count},${fb.uuid},${fb.appType}">
							<td ><a href="javascript:void(0)" onclick="showTr('${fb.appBillCode}','${countStatus.count}','${fb.appType}','${fb.chkMoney}')">${fb.appBillCode}</a></td>
							<td>
							<c:if test="${fb.appType==1}">对外结算</c:if>
							<c:if test="${fb.appType==2}">行业经理</c:if>
							</td>
							<td>
							<c:if test="${!empty fb.appPerson}">
							<script>
							 var url="foreign_findUser.action?timestamp="+ new Date().getTime();
							 var data={saleId:'${fb.appPerson}'};
								$.post(url,data,function(message){
									var msg=message;
									$("#balanceAppList_appperson_"+${countStatus.count}).text(msg);
								})
							 </script>
							 <span id="balanceAppList_appperson_${countStatus.count}"></span>
							 </c:if>
							</td>
							<td>${fb.appSumFee}</td>
						<td><f:parseDate value="${fb.appDate}"
									pattern="yyyyMMddHHmm" var="test" /> <f:formatDate
									value="${test}" pattern="yyyy-MM-dd HH:mm" />
						</td>
						<td>
						<c:if test="${!empty fb.chkPerson}">
						<script>
							 var url="foreign_findUser.action?timestamp="+ new Date().getTime();
							 var data={saleId:'${fb.chkPerson}'};
								$.post(url,data,function(message){
									var msg=message;
									$("#balanceAppList_checkperson_"+${countStatus.count}).text(msg);
								})
							 </script>
							 <span id="balanceAppList_checkperson_${countStatus.count}"></span>
							 </c:if>
						</td>
						<td><f:parseDate value="${fb.chkDate}"
									pattern="yyyyMMddHHmm" var="test" /> <f:formatDate
									value="${test}" pattern="yyyy-MM-dd HH:mm" />
						</td>
						<td>
						<span>
						 <input type="text" value='${fb.chkMoney}' readonly="readonly" id="sumfee${fb.appBillCode}" name="sumfee${fb.appBillCode}"/>
						 <input name="tr_code"  value="${fb.appBillCode}"  type="hidden" />
						</span>
						</td>
						</tr>
                       <tr  id='tr${fb.appBillCode}' style="display: none" >
                       <td colspan="10">
                      <div style="overflow-x: auto;">
                       <table id="table" border="1" width="100%"  class="list">
                       <thead>
                      <c:if test="${fb.appType==1}">
                        <tr >
                       <th rowspan="2">学校名称</th>
                       <th rowspan="2">结算日期</th>
						<th rowspan="2">总人数</th>
						<th colspan="4" style="text-align:center">有效用户数</th>
						<th colspan="2" style="text-align:center">客户经理结算</th>
						<th colspan="2" style="text-align:center">学校现金返还</th>
						<th colspan="2" style="text-align:center">学校话费返还</th>
						<th colspan="2" style="text-align:center">管理员费用</th>
						<th rowspan="2">备注</th>
                       </tr>
                       <tr>
                       	<th>本期</th>
                       	<th>上期</th>
                       	<th>新增</th>
                       	<th>开通率</th>
                       	<th>申请金额</th>
                       	<th>批复金额</th>
                       	<th>申请金额</th>
                       	<th>批复金额</th>
                       	<th>申请金额</th>
                       	<th>批复金额</th>
                       	<th>申请金额</th>
                       	<th>批复金额</th>
                       	</tr>
                       	</c:if>
                       	 <c:if test="${fb.appType==2}">
                        <tr >
                       <th rowspan="2">地区名称</th>
                       <th rowspan="2">结算日期</th>
                       <th rowspan="2">学校数量</th>
						<th rowspan="2">总人数</th>
						<th colspan="4" style="text-align:center">有效用户数</th>
						<th colspan="2" style="text-align:center">行业经理结算</th>
						<th rowspan="2">备注</th>
                       </tr>
                       <tr>
                       	<th>本期</th>
                       	<th>上期</th>
                       	<th>新增</th>
                       	<th>开通率</th>
                       	<th>申请金额</th>
                       	<th>批复金额</th>
                       	</tr>
                       	</c:if>
                       </thead>
				      <tbody id="balanceAppList${fb.appBillCode}">
				      </tbody>
                       </table>
                       </div>
                       </td>
                       </tr>
					</c:forEach>
				</tbody>

			</table>
		</div>
        <%@ include file="/WEB-INF/jsp/public/panelBar.jsp"%>